#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import os

HOST = 'localhost'
PORT = 27017
DB_NAME = 'pmc'
COLLECTION_NAME = 'metalist'


def GetConfig():
    if os.path.exists("config.ini"):
        import ConfigParser
        global HOST
        global PORT
        global DB_NAME
        global COLLECTION_NAME
        config = ConfigParser.ConfigParser()
        config.read("config.ini")
        HOST = config.get("mongodb", "host")
        PORT = int(config.get("mongodb", "port"))
        DB_NAME = config.get("mongodb", "db")
        COLLECTION_NAME = config.get("mongodb", "collection")


if __name__ == '__main__':
    GetConfig()

    connection = MongoClient(HOST, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    outfile = open("manuscript_pmc_ids.txt", "w")

    results = collection.find({"manuscript_id": {"$exists": 1}})
    for result in results:
        outfile.write(str(result["pmcid"]) + "\n")
        outfile.flush()
    outfile.close()
    connection.close()
